$(function(){
    $('.timeCountTd').each(function(index,elm){
        var $this = $(this);
        var firstSpan = $this.find('a:first span')[0];
        if ( firstSpan ) {
            var workdayCommonTd = $this.parent().find('.workdayCommonTd');
            var workdayOverNightTd = $this.parent().find('.workdayOverNightTd');
            var holidayCommonTd = $this.parent().find('.holidayCommonTd');
            var holidayOverNightTd = $this.parent().find('.holidayOverNightTd');

            var tds = [workdayCommonTd, workdayOverNightTd, holidayCommonTd, holidayOverNightTd];
            var allTimeCount = 0;
            $(tds).each(function(){
                var spanF = ($(this).find('span:first'))[0];
                if (spanF) {
                    var spanContent = spanF.innerHTML;
                    var spanContentArr = spanContent.split(':');
                    allTimeCount += spanContentArr[0]*60 + spanContentArr[1]*1;
                }
            });
            var hour = '';
            var minute = '';

            if (allTimeCount<60) {
                hour = '00';
                minute = allTimeCount;
            } else {
                hour = Math.floor(allTimeCount/60);
                minute = allTimeCount - hour*60;
                if (hour<10) {
                    hour = '0'+hour;
                }
                if (minute<10) {
                    minute = '0'+minute;
                }
            }
            firstSpan.innerHTML = hour+':'+minute;
        }
    });

    var $holidayComCount = $('#holidayComCountTd');
    $holidayComCount.data('colCount', 0);
    $('.holidayCommonTd').each(function(){
        colCount($(this), $holidayComCount)
    });
    $holidayComCount[0].innerHTML = countTheReault($holidayComCount.data('colCount'));

    var $holidayNightCount = $('#holidayNightCountTd');
    $holidayNightCount.data('colCount', 0);
    $('.holidayOverNightTd').each(function(){
        colCount($(this), $holidayNightCount)
    });
    $holidayNightCount[0].innerHTML = countTheReault($holidayNightCount.data('colCount'));

    var $workdayComCount = $('#workdayComCountTd');
    $workdayComCount.data('colCount', 0);
    $('.workdayCommonTd').each(function(){
        colCount($(this), $workdayComCount)
    });
    $workdayComCount[0].innerHTML = countTheReault($workdayComCount.data('colCount'));

    var $workdayNightCount = $('#workdayNightCountTd');
    $workdayNightCount.data('colCount', 0);
    $('.workdayOverNightTd').each(function(){
        colCount($(this), $workdayNightCount)
    });
    $workdayNightCount[0].innerHTML = countTheReault($workdayNightCount.data('colCount'));


    var $allCountTd = $('#allCountTd');
    var allTimeCount = $holidayComCount.data('colCount') + $holidayNightCount.data('colCount') +
        $workdayComCount.data('colCount') + $workdayNightCount.data('colCount');
    $allCountTd[0].innerHTML = countTheReault(allTimeCount);



    function colCount($td,$resultTd) {
        var firstSpan = $td.find('span:first')[0];
        if ( firstSpan ) {
            var spanContent = $.trim(firstSpan.innerHTML);
            var timeSplit = spanContent.split(':');
            var colCount = timeSplit[0]*60 + timeSplit[1]*1;
            var lastCount = $resultTd.data('colCount');
            $resultTd.data('colCount', colCount + lastCount);
        }
    }

    function countTheReault($count) {
        $hour = '00';
        $minute = '00';
        if ( $count < 60) {
            $minute = ($count<10) ? '0'+$count : $count;
        } else {
            $hour = Math.floor($count/60);
            $minute = $count - $hour*60;
            $hour = ($hour<10) ? '0'+$hour : $hour;
            $minute = ($minute<10) ? '0'+$minute : $minute;
        }
        return $hour+':'+$minute;
    }
});